<?php
/**
 * author:歪妻
 * Date: 2020/11/2 0002
 */

require_once 'lib/common.php';
session_start();

// 连接到数据库，运行查询，处理错误
$pdo = getPDO();
$posts = getAllPosts($pdo);
$notFound = isset($_GET['not-found']);
?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <title>A blog application</title>
    <?php require 'templates/head.php' ?></head>
<body>
<?php require 'templates/title.php' ?>
<?php if ($notFound): ?>
    <div class="error box">
        错误: 找不到请求的博客文章
    </div>
<?php endif ?>
<div class="post-list">
    <?php foreach ($posts as $post): ?>
        <div class="post-synopsis">
            <h2>
                <?php echo htmlEscape($post['title']) ?>
            </h2>
            <div class="meta">
                <?php echo convertSqlDate($post['created_at']) ?>
                (<?php echo $post['comment_count'] ?> 评论)
            </div>
            <p>
                <?php echo htmlEscape($post['body']) ?>
            </p>
           <div class="post-controls">
               <a href="view-post.php?post_id=<?php echo $post['id']?>">Read more...</a>
               <?php if(isLoggedIn()): ?>|
               <a href="edit-post.php?post_id=<?php echo $post['id']?>">重新编辑</a>
               <?php endif ?>
           </div>
        </div>
    <?php endforeach; ?>
</div>
</body>
</html>
